package com.ooono.factorytest.base;

import static com.ooono.factorytest.model.TestItem.STATE_FAILURE;
import static com.ooono.factorytest.model.TestItem.STATE_SUCCESS;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ooono.factorytest.R;
import com.ooono.factorytest.utils.SPUtils;

public abstract class BaseActivity extends AppCompatActivity {

    private static final String TAG = "BaseActivity";

    private PowerManager.WakeLock mWakeLock = null;

    protected Handler mHandler;

    protected ConstraintLayout bottomButtonCl;
    protected Button successBtn;
    protected Button middleBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        @SuppressLint("InflateParams")
        View root = LayoutInflater.from(this).inflate(R.layout.fragment_test_container, null);
        FrameLayout frameLayout = root.findViewById(R.id.test_container);
        if (getContentLayout() > 0) {
            frameLayout.addView(LayoutInflater.from(this).inflate(getContentLayout(), frameLayout, false));
        }
        bottomButtonCl = root.findViewById(R.id.cl_bottom_button);
        middleBtn = root.findViewById(R.id.btn_middle);
        setContentView(root);
        mHandler = new Handler(getMainLooper());

        if (mWakeLock == null) {
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, this.getClass().getCanonicalName());
            mWakeLock.acquire();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        Button failureBtn = findViewById(R.id.btn_failure);
        failureBtn.setOnClickListener(v -> {
            SPUtils.get(this).saveData(getTestKey(), STATE_FAILURE);
            finish();
        });
        successBtn = findViewById(R.id.btn_success);
        successBtn.setOnClickListener(v -> {
            SPUtils.get(this).saveData(getTestKey(), STATE_SUCCESS);
            finish();
        });
        TextView testTitleTv = findViewById(R.id.tv_test_title);
        if (getTestTitle() == -1) {
            testTitleTv.setVisibility(View.GONE);
        } else {
            testTitleTv.setText(getTestTitle());
        }
        initViews();
    }

    @Override
    protected void onDestroy() {
        if (mWakeLock != null) {
            mWakeLock.release();
        }
        super.onDestroy();
    }

    public abstract int getTestTitle();

    public abstract String getTestKey();

    public abstract int getContentLayout();

    public abstract void initViews();
}
